package com.example.yls.demoa;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button btnInsertstu;
    private Button btnQueryStu;
    private TextView txtResult;
    private Button btnInsertSco;
    private Button btnQuerySco;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnInsertstu = findViewById(R.id.btn_inset_stu);



        btnInsertstu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                ContentValues contentValues = new ContentValues();
                contentValues.put("stuno", "171001");
                contentValues.put("name", "zhangsan");
                contentValues.put("age", 20);
                getContentResolver().insert(uri, contentValues);
            }
        });
        txtResult = findViewById(R.id.text_result);

        btnQueryStu = findViewById(R.id.btn_query_stu);
        btnQueryStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.getCount() > 0) {
                    StringBuffer buffer = new StringBuffer();
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        String name = cursor.getString(1);
                        int age = cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(name).append("  ").append(age).append("\n");
                        txtResult.setText(buffer);
                    }
                }
            }
        });

        btnInsertSco = findViewById(R.id.btn_inset_stu);

        btnInsertSco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                ContentValues contentValues = new ContentValues();
                contentValues.put("stuno", "171001");
                contentValues.put("java", 90);
                contentValues.put("android", 95);
                getContentResolver().insert(uri, contentValues);
            }
        });

        btnQuerySco = findViewById(R.id.btn_query_stu);
        btnQuerySco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.getCount() > 0) {
                    StringBuffer buffer = new StringBuffer();
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        int sjava = cursor.getInt(1);
                        int android = cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(sjava).append("  ").append(android).append("\n");
                        txtResult.setText(buffer);
                    }
                }
            }
        });
    }
}



